package com.csg.service.impl;

import java.util.List;
import java.util.regex.Pattern;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.csg.dao.UserMapper;
import com.csg.entity.User;

import com.csg.service.UserService;


@Service
@Transactional
public class UserServiceImpl implements UserService{
	//注入dao
	@Autowired
	private UserMapper userMapper;
	
	

	@Override
	//实现查询商品列表
	public List<User> selectAllUsers(){
		List<User> user =this.userMapper.selectAllUsers();
		return user;
	}

	@Override
	public User selectUsers(@Param("username")String username,@Param("password")String password) {
		User user =this.userMapper.selectUsers(username,password);
		return user;
	}

	@Override
	public void regist(String username,String password,String email) {
		userMapper.register(username,password,email);
	}
	
	@Override
	public int findUser(String email) {
		if(userMapper.findUser(email)==0){
			return 0;
		}
		return 1;
	}

	public String admitStudentRegister(User user, String againpsd) {
		if (user.getUsername() == null || user.getUsername().equals("")) {
			return "用户名不能为空";
		}
		if (user.getPassword() == null || user.getPassword().equals("")) {
			return "密码不能为空";
		}
		if (againpsd.equals("") || againpsd == null) {
			return "确认密码不能为空";
		}
		if (user.getEmail() == null) {
			return "邮箱不能为空";
		}
		
		Pattern pattern = Pattern.compile("^[a-zA-Z]\\w{5,17}$");
		if (!(pattern.matcher(user.getPassword()).matches())) {
			return "密码以字母开头，长度在6~18之间，只能包含字母、数字和下划线";
		}
		if (!(user.getPassword().equals(againpsd))) {
			return "确认密码填写不一致";
		}
		pattern = Pattern.compile("^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$");
		if (!(pattern.matcher(user.getEmail()).matches())) {
			return "请填写正确的邮箱地址";
		}
		return "0";
	}	
	
	
	/*
	 * 根据学号查询用户名是否存在
	 */
	/*public Boolean findUserByUsername(String username) {
		try {
			User user = UserMapper.getUserByUsername(username);
			if (user != null) {
				return true;// 可以找到该学生
			} else {
				return false;// 不存在该学生
			}
		} catch (Exception e) {
			return false;
		}
	}*/
	//判断用户是否存在
		public boolean isUserExist(String username) {
			if (userMapper.findUsersByUsername(username) == null) {
				return false;
			} else {
				return true;
			}
		}
	 
		//根据提供的用户名拿密码
		public String getPasswordByUsername(String username) {
			return userMapper.findUsersByUsername(username).getPassword();
		}
	 
		//新增一个用户
		public void addUser(String username, String password,String email) {
			User user = new User();
			user.setUsername(username);
			user.setPassword(password);
			user.setEmail(email);
			userMapper.addUser(user);
		}
	  
		//修改某一用户的密码
		public void modifyPasswordByUsernameAndEmail(String username, String newpassword,String email) {
			User user = userMapper.findUsersByUsername(username);
			user.setUsername(username);
			user.setPassword(newpassword);
			user.setEmail(email);
			userMapper.modifyPasswordByUsernameAndEmail(user);
		}


	
}
